package com.lhl.util;

import java.sql.*;

public class ConnUtils {
    //mysql驱动
    private final static String driver = "com.mysql.cj.jdbc.Driver";
    //数据库连接的URL
    private final static String url = "jdbc:mysql://localhost:3306/test00?serverTimezone=UTC";
    //用户名
    private final static String userName = "root";
    //密码
    private final static String passWord = "123456";

    public static Connection getConnection() {
        Connection connection = null;
        try {
            //加载驱动
            Class.forName(driver);
            //获取数据库连接
            connection = DriverManager.getConnection(url, userName, passWord);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return connection;
    }
    public static void close( PreparedStatement preparedStatement, Connection connection) {
        close(null,preparedStatement,connection);
    }
    public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) {
        try {
            if (resultSet != null)
                resultSet.close();
            if(preparedStatement!=null)
                preparedStatement.close();
            if(connection!=null)
                connection.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }
}
